<template>
	<view class="project-board">
		<navBar title="单项目看板"  @goBack="uni.navigateBack()"></navBar>
		
		<scroll-view scroll-y :style="{marginTop: topHeight, height: `calc(100vh - ${extraHeight});`}">
			<GraphicStatistics ref="graphicStatisticsRef" />
			<ScheduleModule ref="scheduleModuleRef" style="margin-top: 15px;" />
			<ImageModule ref="imageModuleRef" style="margin-top: 15px;" />
			<PersonnelManage ref="personnelManageRef" style="margin-top: 15px;" />
			<MaterialsExpend ref="materialsExpendRef" style="margin-top: 15px;" />
			<ScheduleList ref="scheduleListRef" style="margin-top: 15px;" />
		</scroll-view>
	</view>
</template>

<script>
	import GraphicStatistics from './components/GraphicStatistics.vue'
	import ScheduleModule from './components/ScheduleModule.vue'
	import ImageModule from './components/ImageModule.vue'
	import PersonnelManage from './components/PersonnelManage.vue'
	import MaterialsExpend from './components/MaterialsExpend.vue'
	import ScheduleList from './components/ScheduleList.vue'
	import { getProjectOverviewAndProgressStatisticsById, getPlanLineList } from "@/api/comprehensive/comprehensive.js"
	import { reserveDecimalPoint, getSystemHeightInfo } from '@/utils/common.js'
	
	export default {
		components: {
			GraphicStatistics,
			ScheduleModule,
			ImageModule,
			PersonnelManage,
			MaterialsExpend,
			ScheduleList,
		},
		provide() {
			return {
				// 项目名称
				getProjectName: () => this.headerTitle,
				// 项目数据
				getProjectStatisticsData: () => this._projectData || {},
				// 线路列表
				getLineArray: () => this._lineList || [],
				// 项目id
				getId: () => this.id,
			}
		},
		data() {
			return {
				// 尺寸信息
				systemHeightInfo: {}
			}
		},
		computed: {
			// 顶部高度
			topHeight() {
				const { stateHeight, navbarHeight } = this.systemHeightInfo
				return `${(stateHeight + navbarHeight) || 0}px`
			},
			// 额外高度
			extraHeight() {
				const { stateHeight, bottomHeight, navbarHeight } = this.systemHeightInfo
				return `${(stateHeight + bottomHeight + navbarHeight) || 0}px`
			}
		},
		onLoad(options) {
			if (options.name) {
				this.headerTitle = JSON.parse(options.name)
			}
			this.id = Number(options.id)
			this.systemHeightInfo = getSystemHeightInfo()
		},
		onShow() {
			this.init()
		},
		methods: {
			/** 初始化 */
			async init() {
				this.getLineList()
				await this.getProjectData()
				this.$refs.graphicStatisticsRef.init()
				this.$refs.scheduleModuleRef.init()
				this.$refs.imageModuleRef.init()
				this.$refs.personnelManageRef.init()
				this.$refs.materialsExpendRef.init()
				this.$refs.scheduleListRef.init()
			},
	
			/** 获取项目概况和进度统计数据 */
			getProjectData() {
				return new Promise(resolve => {
					if (!this.id) {
						resolve()
						return
					}
					getProjectOverviewAndProgressStatisticsById({ id: this.id }).then(res => {
						if (res && res.result) {
							this._projectData = {
								...res.result,
								planGrossProductNewStr: reserveDecimalPoint(res.result.planGrossProductNew, 10000000000, '9999999999.99'),
								finishedGrossProductStr: reserveDecimalPoint(res.result.finishedGrossProduct, 10000000000, '9999999999.99'),
								overallSchedule: parseInt(parseInt(res.result.complete * 1000) / 10) || 0
							}
						}
					}).finally(() => {
						resolve()
					})
				})
			},
			
			/** 获取线路列表 */
			getLineList() {
				if (!this.id) {
					return
				}
				return new Promise(resolve => {
					getPlanLineList({
						currPage: 1,
						pageSize: 9999,
						params: { pmId: this.id }
					}).then(res => {
						if (res && res.result && Array.isArray(res.result.list)) {
							this._lineList = res.result.list.map(item => ({ ...item, name: item.branchLineName ? `${item.name}（${item.branchLineName}）` : item.name }))
						}
					})
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.project-board {
		min-height: 100vh;
		box-sizing: border-box;
		padding-top: 15px;
		background: #f5f7fa;
		font-size: 16px;
	}
</style>
